package com.exonline.backstage.controller.login;


import com.exonline.backstage.entity.Manager;
import com.exonline.backstage.entity.Menu;
import com.exonline.commons.dao.DaoImpl;
import com.exonline.commons.dao.IDao;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * 后台登陆服务,login方法内,获取用户的姓名,角色,返回动态菜单.
 * @author YUKI
 * @date 2024/11/27
 */
public class LoginService {
    IDao<Object> loginDao = new DaoImpl();

    public Object login(String name, String password) throws SQLException {
        Manager manager = loginDao.queryForObject("SELECT r.id as rid,a.id,a.aname,r.rname FROM admin a JOIN userrole ur on ur.aid = a.id JOIN role r on r.id = ur.rid where aname = ? and password = ?", new BeanHandler<>(Manager.class), name, password);
//        Manager manager = loginDao.queryForObject("SELECT * FROM admin where aname = ? and password = ?", new BeanHandler<>(Manager.class), name, password);
        //查询用户的菜单
        if (manager == null) {
            return null;
        } else {
            List<Menu> parentMenus = loginDao.queryForObject("SELECT DISTINCT m.id AS id,m.mname AS mname, m.murl AS murl,m.mparent AS mparent " +
                    "FROM menu m " +
                    "JOIN rolemenu rm on rm.mid = m.id " +
                    "JOIN role r on r.id = rm.rid " +
                    "JOIN userrole ur on ur.rid = r.id " +
                    "JOIN admin a on a.id = ur.aid WHERE a.id = ? AND m.mparent IS NULL", new BeanListHandler<>(Menu.class), manager.getId());
            List<Menu> subMenus = loginDao.queryForObject("SELECT DISTINCT m.id AS id,m.mname AS mname, m.murl AS murl,m.mparent AS mparent " +
                    "FROM menu m " +
                    "JOIN rolemenu rm on rm.mid = m.id " +
                    "JOIN role r on r.id = rm.rid " +
                    "JOIN userrole ur on ur.rid = r.id " +
                    "JOIN admin a on a.id = ur.aid WHERE a.id = ? AND m.mparent IS NOT NULL", new BeanListHandler<>(Menu.class), manager.getId());
            for (Menu parentMenu : parentMenus) {
                String parentId = parentMenu.getId();
                for (Menu subMenu : subMenus) {
                    if (subMenu.getMparent().equals(parentId)) {
                        parentMenu.addSubMenus(subMenu);
                    }
                }
            }
            System.out.println(parentMenus);
            manager.setMenus(parentMenus);
            loginDao.updateWithTransaction("UPDATE admin set lastlogintime = CURRENT_TIMESTAMP WHERE id = ?", manager.getId());
            return manager;
        }
    }
    public Object loginWithUserName(String phone, String password) throws SQLException {
        Manager manager = loginDao.queryForObject("SELECT r.id as rid,a.id,a.aname,r.rname  FROM admin a JOIN userrole ur on ur.aid = a.id JOIN role r on r.id = ur.rid where aname = ? and password = ?", new BeanHandler<>(Manager.class), phone, password);
        //查询用户的菜单
        if (manager == null) {
            return null;
        } else {
            List<Menu> parentMenus = loginDao.queryForObject("SELECT DISTINCT m.id AS id,m.mname AS mname, m.murl AS murl,m.mparent AS mparent " +
                    "FROM menu m " +
                    "JOIN rolemenu rm on rm.mid = m.id " +
                    "JOIN role r on r.id = rm.rid " +
                    "JOIN userrole ur on ur.rid = r.id " +
                    "JOIN admin a on a.id = ur.aid WHERE a.id = ? AND m.mparent IS NULL", new BeanListHandler<>(Menu.class), manager.getId());
            List<Menu> subMenus = loginDao.queryForObject("SELECT DISTINCT m.id AS id,m.mname AS mname, m.murl AS murl,m.mparent AS mparent " +
                    "FROM menu m " +
                    "JOIN rolemenu rm on rm.mid = m.id " +
                    "JOIN role r on r.id = rm.rid " +
                    "JOIN userrole ur on ur.rid = r.id " +
                    "JOIN admin a on a.id = ur.aid WHERE a.id = ? AND m.mparent IS NOT NULL", new BeanListHandler<>(Menu.class), manager.getId());
            for (Menu parentMenu : parentMenus) {
                String parentId = parentMenu.getId();
                for (Menu subMenu : subMenus) {
                    if (subMenu.getMparent().equals(parentId)) {
                        parentMenu.addSubMenus(subMenu);
                    }
                }
            }
            System.out.println(parentMenus);
            manager.setMenus(parentMenus);
            return manager;
        }
    }

    public int register(String id, String name, String password) throws SQLException {
        return loginDao.updateWithTransaction("INSERT INTO admin values(id = ?, aname = ?, password = ?)", id, name, password);
    }
}
